<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px"
				 label-position="left">
			<el-form-item label="&nbsp;&nbsp;用户头像" prop="headPortraitCompleteUrl">
				<sc-upload :apiObj="$API.system.user.uploadHeadPortrait" v-model="form.headPortraitCompleteUrl" title="上传头像" :cropper="true" :compress="1" :aspectRatio="1/1"></sc-upload>
			</el-form-item>
			<el-form-item label="用户昵称" prop="nickname">
				<el-input v-model="form.nickname" placeholder="请输入完整的真实姓名" clearable :readonly="readOnly"></el-input>
			</el-form-item>
			<el-form-item label="登录账号" prop="username">
				<el-input v-model="form.username" placeholder="用于登录系统" clearable :readonly="readOnly"></el-input>
			</el-form-item>
			<el-form-item label="所属角色" prop="roleIds">
				<el-select v-if="readOnly" v-model="form.roleIds" multiple filterable style="width: 100%" :limit="1">
					<el-option v-for="item in roleDropDownList" :key="item.roleId" :label="item.name"
							   :value="item.roleId" disabled/>
				</el-select>
				<el-select v-else v-model="form.roleIds" multiple filterable style="width: 100%" >
					<el-option v-for="item in roleDropDownList" :key="item.roleId" :label="item.name"
							   :value="item.roleId"/>
				</el-select>
			</el-form-item>
			<template v-if="mode=='add'">
				<el-form-item label="登录密码" prop="password">
					<el-input type="password" v-model="form.password" clearable show-password></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="password2">
					<el-input type="password" v-model="form.password2" clearable show-password></el-input>
				</el-form-item>
			</template>
			<el-form-item class="to-them" label="状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态" prop="status">
				<el-select v-if="readOnly" v-model="form.status" placeholder="请选择" :readonly="!form.canDelete">
					<el-option label="有效" value="1" disabled></el-option>
					<el-option label="锁定" value="3" disabled></el-option>
				</el-select>
				<el-select v-else v-model="form.status" placeholder="请选择" :readonly="!form.canDelete">
					<el-option label="有效" value="1"></el-option>
					<el-option label="锁定" value="3"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item class="to-them" label="性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别" prop="sex">
				<el-select v-model="form.sex" placeholder="请选择">
					<el-option label="保密" value="3"></el-option>
					<el-option label="男" value="1"></el-option>
					<el-option label="女" value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item class="to-them" label="手&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;机" prop="phone">
				<el-input v-model="form.phone" placeholder="请输入手机号" clearable></el-input>
			</el-form-item>
			<el-form-item class="to-them" label="邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱" prop="email">
				<el-input v-model="form.email" placeholder="请输入邮箱" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增用户',
				edit: '编辑用户',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			readOnly: false,
			form: {
				userId: "",
				username: "",
				headPortraitCompleteUrl: "",
				nickname: "",
				sex: "3",
				status: "1",
				phone: "",
				email: "",
				roleIdMany: "",
				headPortrait: "",
				roleIds: []
			},
			//验证规则
			rules: {
				headPortraitCompleteUrl: [
					{required: false, message: '请上传头像'}
				],
				username: [
					{required: true, message: '请输入登录账号'}
				],
				nickname: [
					{required: true, message: '请输入真实姓名'}
				],
				sex: [
					{required: false, message: '请选择性别'}
				],
				phone: [
					{required: false, message: '请输入手机号'}
				],
				email: [
					{required: false, message: '请输入邮箱'}
				],
				status: [
					{required: false, message: '请选择账号状态'}
				],
				password: [
					{required: true, message: '请输入登录密码'},
					{
						validator: (rule, value, callback) => {
							if (this.form.password2 !== '') {
								this.$refs.dialogForm.validateField('password2');
							}
							callback();
						}
					}
				],
				password2: [
					{required: true, message: '请再次输入密码'},
					{
						validator: (rule, value, callback) => {
							if (value !== this.form.password) {
								callback(new Error('两次输入密码不一致!'));
							} else {
								callback();
							}
						}
					}
				],
				roleIds: [
					{required: true, message: '请选择所属角色', trigger: 'change'}
				]
			},

			//所需数据选项
			roleDropDownList: [],
		}
	},
	mounted() {
		this.getInitData()
	},
	methods: {

		/***
		 * 显示当前组件窗口
		 * @param mode
		 * @returns {default.methods}
		 */
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},

		/**
		 * 加载初始化数据
		 * @returns {Promise<void>}
		 */
		async getInitData() {
			// 获取角色列表数据
			var res = await this.$API.system.role.listAll.get();
			this.roleDropDownList = res.data;
		},

		/**
		 * 表单提交方法
		 */
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var headPortrait = this.form.headPortraitCompleteUrl;
					if (this.form.headPortraitCompleteUrl) {
						let fileId = "";
						let paths = headPortrait.split("/");
						let len = 4;
						for (let i = paths.length - 1; i > 0 && len > 0; i--, len--) {
							if (len == 1) {
								fileId = paths[i] + fileId;
								continue;
							}
							fileId = "/" + paths[i] + fileId;
						}
						let indexOf = fileId.indexOf("?");
						if(indexOf != -1){
							fileId = fileId.substring(0, indexOf);
						}
						this.form.headPortrait = fileId;
					}

					var res;
					if (this.mode == 'add') {
						res = await this.$API.system.user.add.post(this.form);
					} else {
						res = await this.$API.system.user.edit.put(this.form);
					}
					this.isSaveing = false;
					if (res.status == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					}
				} else {
					return false;
				}
			})
		},

		/**
		 * 表单注入数据
		 * @param data
		 */
		setData(data) {
			this.form.userId = data.userId
			this.form.username = data.username
			this.form.nickname = data.nickname
			this.form.sex = data.sex.toString()
			this.form.phone = data.phone
			this.form.email = data.email
			this.form.status = data.status.toString()
			this.form.headPortraitCompleteUrl = data.headPortrait
			this.form.canDelete = data.canDelete
			for (let role of data.roles) {
				this.form.roleIds.push(role.roleId)
			}
			this.readOnly = data.canDelete == 0

			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
